package edu.bw.web.dept;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.service.DeptService;
import edu.bw.service.impl.DeptServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;

@WebServlet("/dept/deleteDeptByIds")
public class DeleteDeptServlet extends HttpServlet {
    DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{"ids":[7]}
        //1.解析前端发送的数据
        String reqJSON = request.getReader().readLine();
        List<Integer> ids = JSONUtil.parseArray((JSONUtil.parseObj(reqJSON).get("ids"))).toList(Integer.class);

        //2.调用service层处理数据
        Integer rows = deptService.deleteDeptByIds(ids);

        //3.向前端响应
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
